package com.atguigu.product.products.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.atguigu.product.products.dao.PmsCategoryDao;
import com.atguigu.product.products.entity.PmsAttrEntity;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.atguigu.product.products.entity.PmsCategoryEntity;
import com.atguigu.product.products.service.PmsCategoryService;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.R;


/**
 * 商品三级分类
 *
 * @author qhw
 * @email qhw@gmail.com
 * @date 2021-08-03 19:57:10
 */
@RestController
@RequestMapping("product/category")
public class PmsCategoryController {
    @Autowired
    private PmsCategoryService pmsCategoryService;
    @Autowired
    private PmsCategoryDao pmsCategoryDao;

    /**
     * 列表--递归查询所有数据
     */
    @RequestMapping("/list/tree")
    //@RequiresPermissions("products:pmscategory:list")
    public R list() {
        //PageUtils page = pmsCategoryService.queryPage(params);
        List<PmsCategoryEntity> categoryEntities = pmsCategoryService.queryByTree();
        return R.ok().put("data", categoryEntities);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{catId}")
    //@RequiresPermissions("products:pmscategory:info")
    public R info(@PathVariable("catId") Long catId) {
        PmsCategoryEntity pmsCategory = pmsCategoryService.getById(catId);

        return R.ok().put("data", pmsCategory);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("products:pmscategory:save")
    public R save(@RequestBody PmsCategoryEntity pmsCategory) {
        pmsCategoryService.save(pmsCategory);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    // @RequiresPermissions("products:pmscategory:update")
    public R update(@RequestBody PmsCategoryEntity pmsCategory) {
        pmsCategoryService.updateDetail(pmsCategory);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("products:pmscategory:delete")
    public R delete(@RequestBody Long[] catIds) {
        pmsCategoryService.deleteAllPitch(Arrays.asList(catIds));
        return R.ok();
    }

    /**
     * 测试open-feign的调用---调用商品服务查询一号商品的信息
     */
    @GetMapping("/testOne")
    public R testOne() {
        PmsCategoryEntity pmsCategoryEntity = pmsCategoryDao.selectById(1);
        return R.ok().put("page", pmsCategoryEntity);
    }


    /**
     * 压力测试
     */
    @GetMapping("/test")
    public String test() {
        return "hello";
    }

}
